<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
          <!--<a-button  type="primary" preIcon="ant-design:plus-outlined"  v-auth="'user:add'" @click="handleCreate" > 新增</a-button> -->
          <a-button  type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="handleBatchForceLogout">
                <Icon icon="ant-design:delete-outlined"></Icon>
                强退
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="online-user" setup>
  // ts语法
  import {ref, computed, unref} from 'vue';
  import { BasicTable, TableAction, ActionItem } from '/@/components/Table'
  import {useDrawer} from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage'
  import {useModal} from '/@/components/Modal';
  import {useMessage} from '/@/hooks/web/useMessage';
  import {columns, searchFormSchema} from './user.data';
  import {list, forceLogout, forceLogoutBatch, getImportUrl,getExportUrl} from './user.api';
  import { Icon } from '/@/components/Icon';

  // import { usePermission } from '/@/hooks/web/usePermission'
  // const { hasPermission } = usePermission();

  const { createMessage, createConfirm } = useMessage();

  const selectRows = ref([]);

  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    designScope: 'online-user-list',
    tableProps: {
      title: '在线用户列表',
      api: list,
      columns: columns,
      size: 'small',
      bordered: false,
      formConfig: {
        labelWidth: 100,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
      },
      beforeFetch:(params)=>{
        return Object.assign({'column':'createTime','order':"desc"},params)
      },
    },
    exportConfig: {
      name:"在线用户列表",
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl
    },
  })

  // 注册table数据
  const [registerTable, {reload, updateTableDataRecord},{ rowSelection, selectedRowKeys }] = tableContext

  /**
   * 强制用户退出
   */
  async function handleForceLogout(record: Recordable) {
    console.log(record);
    await forceLogout({userId: record.id}, reload);
  }
  /**
   * 批量删除事件
   */
  async function handleBatchForceLogout() {
    await forceLogoutBatch({ userIds: selectedRowKeys.value }, () => {
      selectedRowKeys.value = []
      reload()
    })
  }
  /**
   * 成功回调
   */
  function handleSuccess({isUpdate, values}) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }

  /**
   * 操作栏
   */
  function getTableAction(record): ActionItem[] {
    return [
      {
        label: '强退',
        onClick: handleForceLogout.bind(null, record)
      }
    ]
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
       
    ]
  }

</script>

<style scoped>

</style>
